package com.yulongtian.interviewWorks.day20220824;

/**
 * https://leetcode.cn/problems/kth-node-from-end-of-list-lcci/
 *
 * @author YulongTian
 * @date 2022/8/24 - 16:09
 */
public class InterviewsWork06 {
    public static void main(String[] args) {

    }

    public int kthToLast(ListNode head, int k) {

        ListNode head2=new ListNode(-1);

        while (head!=null){
            ListNode temp=head;
            head=head.next;
            temp.next=head2.next;
            head2.next=temp;
        }

        int len=0;
        while (head2.next!=null){
            len++;
            if(len==k){
                return head2.next.val;
            }
            head2=head2.next;
        }
        return -1;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
